﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PeekService;

namespace PeekWebApp.handler
{
    /// <summary>
    /// DetailHandler 的摘要说明
    /// </summary>
    public class DetailHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            string action = context.Request["action"];
            switch (action)
            {
                case "Msg":
                    try
                    {
                        string username = CommonService.EncodeSQLStr(CommonService.HTMLEncode(context.Request["username"].Trim()));
                        string email = CommonService.EncodeSQLStr(CommonService.HTMLEncode(context.Request["email"].Trim()));
                        string msg = CommonService.EncodeSQLStr(CommonService.HTMLEncode(context.Request["msg"].Trim()));
                        UserMessage userMessage = new UserMessage();
                        userMessage.Code = Guid.NewGuid().ToString();
                        userMessage.CreateID = "";
                        userMessage.CreateTime = DateTime.Now;
                        userMessage.Email = email;
                        userMessage.Message = msg;
                        userMessage.Name = username;
                        userMessage.Remark = "";
                        userMessage.Sort = new ExtendService().GetSort("UserMessage");
                        userMessage.State = 0;
                        new UserMessageService().Add(userMessage);
                        string str = "<b>姓名：</b>" + username + "<br/><b>邮箱：</b>" + email + "<br/><b>留言内容：</b>" + msg;
                        CommonService.SendEmail(email, str, "网站留言簿", SendType.Message);
                        context.Response.Write(CommonService.ConvertToJSON("1"));
                    }
                    catch
                    {
                        context.Response.Write(CommonService.ConvertToJSON("0"));
                    }
                    break;
                case "Get":
                    string code = context.Request["id"];
                    int result = 0;
                    ExtendService ext = new ExtendService();
                    ext.UpdateHits(code);
                    PeekService.TourInfoModel model = new PeekService.ExtendService().GetAllByTypeRegion(1, 1, out result, "", "", code, "", "")[0];
                    List<SysFile> fileList = new List<SysFile>();
                    fileList = ext.GetFileByTourismCode(code);
                    MapInfo mapInfo = ext.GetMapByTourismCode(code);
                    mapInfo.Content = context.Server.HtmlDecode(mapInfo.Content);
                    //List<TicketInfo> ticketList = ext.GetTicketInfoByTourismCode(code);
                    //context.Response.Write(PeekService.CommonService.ConvertToJSON(new { model, file = fileList,map=mapInfo,ticket=ticketList }));
                    List<TicketInfo> ticketList = ext.GetTicketInfoByTourismCode(code);
                    context.Response.Write(PeekService.CommonService.ConvertToJSON(new { model, file = fileList, map = mapInfo }));
                    break;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}